package com.example.studentdemo.controller;

import com.example.studentdemo.service.AvatarService;
import com.example.studentdemo.common.Result;
import io.minio.errors.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Slf4j
@RestController
@Tag(name = "文件上传模块", description = "文件上传模块接口")
public class FileController {
    @Resource
    private AvatarService avatarService;

    @PostMapping("/avatar/upload")
    @Operation(summary = "头像上传", description = "头像上传接口")
    @Parameter(description = "头像", name = "file", required = true)
    public Result upload(@RequestParam("file") MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        log.info("文件上传：{}", file.getOriginalFilename());
        String avatarUrl = avatarService.uploadAvatar(file);
        return Result.success(avatarUrl);
    }
}
